package com.base.app_core;

import android.app.Application;
import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.app_core.ioc.component.BaseAppComponent;
import com.base.app_core.service.user.LoginCallback;
import com.base.app_core.service.user.model.User;
import com.base.app_core.util.AppConstants;
import com.blankj.utilcode.util.Utils;
import com.tencent.bugly.Bugly;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.inject.Inject;

import cn.jpush.android.api.JPushInterface;


//import cn.jpush.android.api.JPushInterface;

/**
 * Created by xiangheng.song on 2017/2/28.
 */

public abstract class App extends Application {


    private static ExecutorService newFixedThreadPool = Executors
            .newFixedThreadPool(6);

    public static ExecutorService getNewFixedThreadPool() {
        return newFixedThreadPool;
    }

    private static App app;

    public static LoginCallback lastCallback;

    public abstract String getServerUrl();

    private static Context context;

    private static int mainTid;


    public static Context getContext() {
        return context;
    }

    protected BaseAppComponent appComponent = null;

    public BaseAppComponent getAppComponent() {
        return appComponent;
    }

    public void setupAppConment() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        AppConstants.SERVER_URL = getServerUrl();
        context = getApplicationContext();
        mainTid = android.os.Process.myTid();
        setupAppConment();
        initThirdServer();
        UMConfigure.init(context, "5addf058f43e4807bc0005f0", "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setLogEnabled(true);
        PlatformConfig.setWeixin("wx9c852be300314953", "05d2224a547bb2386f290a700325bdf9");
    }

    @Inject
    User user;
    public IWXAPI iwxapi;

    public void regToWx() {
        iwxapi = WXAPIFactory.createWXAPI(getContext(), getWxAppID(), true);
        iwxapi.registerApp(getWxAppID());
    }

    protected void initThirdServer() {
        //todo leakcanary20180803检测内存泄漏
        Bugly.init(this, getBugLyID(), false);
        Bugly.setIsDevelopmentDevice(this, BuildConfig.DEBUG);
        Utils.init(this);//一个utils库的初始化 https://github.com/Blankj/AndroidUtilCode/blob/master/README-CN.md
        ARouter.openDebug();
        ARouter.init(this);
        ARouter.openLog();  // 开启日志
        regToWx();
        QbSdk.initX5Environment(getContext(), null);
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();

    }

    public static App getApp() {
        return app;
    }

    public static int getMainTid() {
        return mainTid;
    }

    public abstract String getBugLyID();

    public abstract String getWxAppID();

}
